package net.tngou.iask.action.my;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.pojo.Access;
import net.tngou.iask.pojo.Accessresult;
import net.tngou.iask.pojo.Analysis;
import net.tngou.iask.pojo.Analysis_patient;
import net.tngou.iask.pojo.Health;
import net.tngou.iask.pojo.Patient;
import net.tngou.iask.pojo.Question;
import net.tngou.iask.service.AccessService;
import net.tngou.iask.service.AnalysisService;
import net.tngou.iask.service.CureService;
import net.tngou.iask.service.PatientService;
import net.tngou.iask.service.QuestionService;
import net.tngou.iask.util.IDUtil;
import net.tngou.iask.util.StringUtil;

/**
 * 定制随访
 * @author tngou@tngou.net
 *
 */
public class AnalysisAction extends BaseAction {

	/**
	 * 定制随访列表
	 */
	@Override
	public void execute() throws ServletException, IOException {
		
		PageUtil page = service.getPage(ask.getPage(),ask.getSize(),"user="+user.getId(), Analysis.class);
		root.put("page", page);
		printFreemarker("my/analysis.ftl", root);
	}
	
	/**
	 * 添加自定义随访
	 */
	public void add() {
		
		printFreemarker("my/analysis_add.ftl", root);
	}
	
	public void update() {
		
		if(ask.getId()==0){run_404();return;}
		Analysis analysis = new Analysis().get(ask.getId());
		if(analysis==null||analysis.getUser()!=user.getId()){run_404();return;}
		root.put("analysis", analysis);		
		printFreemarker("my/analysis_update.ftl", root);
	}
	
	/**
	 * 患者入组
	 */
	public void patientadd() {
		if(ask.getId()==0){run_404();return;}
		Analysis analysis = new Analysis().get(ask.getId());
		if(analysis==null||analysis.getUser()!=user.getId()){run_404();return;}
//		CureService service = new CureService();	
//		PageUtil page = service.getPage(ask.getPage(), ask.getPage(), user.getMain(), ask.getKeyword());
		AnalysisService service = new AnalysisService();
		PageUtil page = service.getCurePage(ask.getPage(), ask.getSize(), user.getMain(),analysis.getId(), ask.getKeyword());
		root.put("page", page);
		root.put("analysis", analysis);	
		root.put("IDUtil", new IDUtil());
		printFreemarker("my/analysis_patientadd.ftl", root);
	}
	
	/**
	 * 统计参数
	 */
	public void questionadd() {
		if(ask.getId()==0){run_404();return;}
		Analysis analysis = new Analysis().get(ask.getId());
		if(analysis==null||analysis.getUser()!=user.getId()){run_404();return;}
		AnalysisService service = new AnalysisService();
		PageUtil page = service.getQuestionPage(ask.getPage(), ask.getSize(), user.getMain(),analysis.getId(), ask.getKeyword());
		root.put("page", page);
		root.put("analysis", analysis);		
		printFreemarker("my/analysis_questionadd.ftl", root);
	}
	
	/**
	 * 入组病人修改
	 */
	public void patientupdate() {
		if(ask.getId()==0){run_404();return;}
		Analysis_patient analysis_patient = new Analysis_patient().get(ask.getId());
		if(analysis_patient==null){run_404();return;}
		Analysis analysis = new Analysis().get(analysis_patient.getAnalysis());
		Patient patient = new Patient().get(analysis_patient.getPatient());
		if(analysis==null||patient==null||analysis.getUser()!=user.getId()){run_404();return;}
		root.put("analysis_patient", analysis_patient);
		root.put("analysis", analysis);	
		root.put("patient", patient);	
		printFreemarker("my/analysis_patientupdate.ftl", root);
	}
	
	/**
	 * 查看入组患者
	 */
	public void patient() {
		if(ask.getId()==0){run_404();return;}
		Analysis analysis = new Analysis().get(ask.getId());
		if(analysis==null||analysis.getUser()!=user.getId()){run_404();return;}
		AnalysisService service = new AnalysisService();
		PageUtil page = service.getPatient(analysis.getId(), ask.getPage(), ask.getSize(), ask.getKeyword());
		root.put("page", page);
		root.put("analysis", analysis);	
		root.put("IDUtil", new IDUtil());
		printFreemarker("my/analysis_patient.ftl", root);
	}
	
	/**
	 * 查看统计模板
	 */
	public void question() {
		if(ask.getId()==0){run_404();return;}
		Analysis analysis = new Analysis().get(ask.getId());
		if(analysis==null||analysis.getUser()!=user.getId()){run_404();return;}
		AnalysisService service = new AnalysisService();
		List<Question> list = service.getQuestion(analysis.getId());
		root.put("list", list);
		root.put("analysis", analysis);	
		printFreemarker("my/analysis_question.ftl", root);
	}
	
	/**
	 * 显示自定义统计
	 */
	public void show() {
		if(ask.getId()==0){run_404();return;}
		Analysis analysis = new Analysis().get(ask.getId());
		if(analysis==null||analysis.getUser()!=user.getId()){run_404();return;}
		Map<String, String> map = new HashMap<>();
		AnalysisService service = new AnalysisService();
		List<Map<String, Object>> patients = service.getPatient(analysis.getId());
		List<Question> questions = service.getQuestion(analysis.getId());
		
		List<Health> list = service.getHeath(patients);
		list.forEach(e->{
			String key=e.getPatient()+"_"+e.getQuestion();
			String value = map.get(key);
			if(StringUtil.isEmpty(value))
			{
				map.put(key, e.getDescription());
			}else
			{
				map.put(key, value+" "+e.getDescription());
			}
		});
		
		root.put("map", map);
		root.put("patients", patients);
		root.put("questions",questions);
		root.put("analysis", analysis);	
		printFreemarker("my/analysis_show.ftl", root);
	}
	
	
	/**
	 * 病人信息
	 */
	@SuppressWarnings("unchecked")
	public void patientshow() {
		long id=0;
		String[] params = request.getParams();
		if(params!=null&&params.length>1&&NumberUtils.isNumber(params[1]))
			id=Long.parseLong(params[1]);
		if(ask.getId()==0||id==0){run_404();return;}
		Patient patient =new Patient().get(id);
		Analysis analysis = new Analysis().get(ask.getId());
		if(patient==null||analysis==null||analysis.getUser()!=user.getId()){run_404();return;}
		
		AnalysisService service = new AnalysisService();
		List<Question> questions = service.getQuestion(analysis.getId());
		
		List<Access> accesses = (List<Access>) service.getList("main="+user.getMain()+" AND patient="+patient.getId(), Access.class);
		AccessService accessService = new AccessService();
		List<Accessresult> list = accessService.getAccessresult(patient.getId(), accesses);
		Map<String, String> map = new HashMap<>();
		list.forEach(e->{
			String key=e.getAccess()+"_"+e.getQuestion();
			String value = map.get(key);
			if(StringUtil.isEmpty(value))
			{
				map.put(key, e.getDescription());
			}else
			{
				map.put(key, value+" "+e.getDescription());
			}
		});
		root.put("accesses", accesses);
		root.put("map", map);
		root.put("analysis", analysis);	
		root.put("patient", patient);	
		root.put("questions",questions);
		printFreemarker("my/analysis_patientshow.ftl", root);
		
	}
	
	
	/**
	 * 问题统计
	 */
	public void questionshow() {
		long id=0;
		String[] params = request.getParams();
		if(params!=null&&params.length>1&&NumberUtils.isNumber(params[1]))
			id=Long.parseLong(params[1]);
		if(ask.getId()==0||id==0){run_404();return;}
		Question question =new Question().get(id);
		Analysis analysis = new Analysis().get(ask.getId());
		if(question==null||analysis==null||analysis.getUser()!=user.getId()){run_404();return;}
		
		AnalysisService service = new AnalysisService();
		List<Map<String, Object>> patients = service.getPatient(analysis.getId());	
		Map<String, String> map = new HashMap<>();
		List<Health> list = service.getHeath(patients);
		list.forEach(e->{
			if(e.getQuestion()==question.getId())
			{
				String key=e.getPatient()+"_"+e.getQuestion();
				String value = map.get(key);
				if(StringUtil.isEmpty(value))
				{
					map.put(key, e.getDescription());
				}else
				{
					map.put(key, value+" "+e.getDescription());
				}
			}
		});
		
		AccessService accessService = new AccessService();
		float[] rs= new float[7];
		rs[0]=patients.size();
		rs[1]=accessService.access(user.getMain(), question.getId(), patients);
		if(rs[0]>0)rs[2]=rs[1]/rs[0];

		rs[3]=accessService.accessMax(user.getMain(), question.getId(), patients);
		rs[4]=accessService.accessMin(user.getMain(), question.getId(), patients);
		rs[5]=accessService.accessresult(user.getMain(), question.getId(), patients);
		rs[6]=accessService.accessresultvalue(user.getMain(), question.getId(), patients);
		root.put("rs", rs);
		root.put("map", map);
		root.put("patients", patients);
		root.put("analysis", analysis);	
		root.put("question", question);	
		printFreemarker("my/analysis_questionshow.ftl", root);
		
	}
	
	
	/**
	 * 死亡 统计
	 */
	public void obituary() {
		if(ask.getId()==0){run_404();return;}
		Analysis analysis = new Analysis().get(ask.getId());
		if(analysis==null||analysis.getUser()!=user.getId()){run_404();return;}
		
		
		
		printFreemarker("my/analysis_obituary.ftl", root);
	}
}
